//按需加载js文件封装函数 BEGIN
function includeScript(url, callback){
  var script = document.createElement("script"); 
  var doc = document.getElementsByTagName("script")[0]; 
  script.type = "text/javascript"; 
  script.src = url; 
  doc.parentNode.insertBefore(script, doc); 
  if (script.readyState) { //IE 
    script.onreadystatechange = function(){
      if (script.readyState == "loaded" || script.readyState == "complete"){
         script.onreadystatechange = null; 
         if(callback){callback()}; 
        } 
    }; 
  }else{ //标准的DOM浏览器 
      script.onload = function(){
         if(callback){callback()}; 
    }; 
  } 
}

// 监听浏览器窗口大小变化。处理DOM位置
var listenResize = function(prop){
  listenResize.throttleTime = prop.throttle || 20;
  clearTimeout(listenResize.timer);
  listenResize.timer = setTimeout(function(){
    var w = document.documentElement.clientWidth;
    if(w <= 480 && listenResize.c != 480){
      listenResize.c = 480;
      prop.w480 && prop.w480();
    }else if(w > 480 && w <= 780 && listenResize.c != 780){
      listenResize.c = 780;
      prop.w780 && prop.w780();
    }else if(w > 780 && w <=1200 && listenResize.c != 1200){
      listenResize.c = 1200;
      prop.w1200 && prop.w1200();
    }else if(w >1200 & listenResize.c != 1920){
      listenResize.c = 1920;
      prop.w1920 && prop.w1920();
    }
  },listenResize.throttleTime);
}


function SiteSearch() {
    var t = "", s = "",ch = "";
    if ($("#keyword").val() == "找不到，搜索有惊喜！") {
        alert("请填写查询内容");
        return;
    }
    if ($("#channelId") != null && $("#channelId").val() != "") {
        ch = $("#channelId").val() + "/";
    }
    if ($("#keyword").val() != "") {
        s = encodeURIComponent($.trim($("#keyword").val()));
    }
    else {
        alert("请填写查询内容");
        return;
    }
    // alert("search.aspx?ch="+ch+"&s="+s+"&t="+t);
    location.href = "/search/" + ch + s;
    //alert($("#q1").val());
}


$(function () {// 页面加载后执行部分 

    //图片延迟加载
    $("img.lazy").lazyload({
        effect: "fadeIn"
    });


    /**
    *顶部模块
    */

    //二级菜单效果
    $(".lp-header .nav li.nav-sort").hover(function () {
        $(this).addClass("hover");
        $(this).children(".nav-sort-ej").show({
            duration: 200,
            easing: 'easeOutBack'
        });
    }, function () {
        $(this).removeClass("hover");
        $(this).children(".nav-sort-ej").hide();
    })


    // 手机版二级导航


    $("#lp-topP-sort").click(function () {
        $("#lp-topP-sort-ej").height($(window).height() - 60);
        if ($("#lp-topP-sort-ej").css("display") == "none") {
            $(this).addClass("show");
            $("#lp-topP-sort-ej").slideDown({
                duration: 300,
                easing: 'easeOutCirc'
            });
        } else {
            $(this).removeClass("show");
            $("#lp-topP-sort-ej").slideUp({
                duration: 300,
                easing: 'easeOutCirc'
            });
        }
        $("#lp-topP-sort-ej").find(".close").click(function () {
            $("#lp-topP-sort").removeClass("show");
            $("#lp-topP-sort-ej").slideUp({
                duration: 300,
                easing: 'easeOutCirc'
            });
        })

    })

    // 手机版滑动屏幕收起搜索
    $(document).on('touchstart', function () {
        $(".lp-header .phone-nav .phone-nav-search input").blur();
    });
    $('.phone-nav').on('touchstart', function (e) {
        e.stopPropagation();
    });

    // 阻止手机版导航下拉时，滑动屏幕内容区滚动
    $('.phone-nav-ej').on('touchmove', function (e) {
        e.preventDefault();
    });

    $('.phone-nav-search input').on({
        focus: function () {
            $('.lp-logo-wp').css('zIndex', 0);
        }, blur: function () {
            $('.lp-logo-wp').css('zIndex', 1000);
        }
    });


    // 搜索框自动补全 Start
    $("#lp-top-search input").bind("input propertychange click", function (event) {
        // alert(11)
        if (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 27) return; //向下 向上 esc 就返回
        var search_str = $(this).val();
        if (search_str == "") {
            // $("#lp-top-search .tip-ul").removeClass("show").addClass("hide").text("");
            // return;
            search_str = "WI_Th_O~t_k^ey+word";
        }
        var list = [];
        $.ajax({
            type: "get",
            // dataType:"html",
            dataType: "jsonp",
            url: BASE_URL + "search/suggestion/str/" + search_str,
            success: function (data) {
                //这里写拿到提示数据的数组存入list
                // list = msg.list;
                // if(typeof(list.length) == 'undefined'){alert(1);$("#lp-top-search .tip-ul").addClass("hide");}
                $("#lp-top-search .tip-ul").text("").append(data.html).css({ "display": "none" }).slideDown(100);
                // for(var i=0; i<list.length; i++){
                //   $("<li>").addClass("search-li").text(list[i]).appendTo("#lp-top-search .tip-ul");
                // }
                // $("#lp-top-search .tip-ul").append($("<li>").addClass("delete-li").text("智能搜索提示").append($("<a>").attr("href","javascript:;").text("X")));
                $("#lp-top-search .tip-ul").removeClass("hide").addClass("show");
            }
        });
    })


    // 鼠标移到li样式
    $("#lp-top-search .tip-ul li").live('mouseover mouseout', function (event) {
        if (event.type == 'mouseover') {
            // 每次触发事件就去掉键盘事件样式
            $("#lp-top-search .tip-ul .search-li").removeClass("key");
            $(this).addClass("cur");
        } else {
            $(this).removeClass("cur");
        }
    });


    // 鼠标收起提示列表
    $("#lp-top-search .tip-ul .delete-li a").die().live('click', function (event) {
        $("#lp-top-search .tip-ul").removeClass("show").addClass("hide").text("");
        event.stopPropagation();
    })


    // 鼠标点击自动补全内容表单提交 
    $("#lp-top-search .tip-ul .search-li").die().live('click', function (event) {
        $("#lp-top-search input").val($(this).text());
        $("#lp-top-search form").submit();
        event.stopPropagation();
    })



    // 键盘keydown事件
    var com_topSch_cur = -1;
    $("#lp-top-search input").bind("keydown", function (evt) {
        evt = (evt) ? evt : window.event;
        if ($("#lp-top-search .tip-ul").find("li").eq(0).text()) {//如果有补全列表了
            var key_length = $("#lp-top-search .tip-ul .search-li").length;
            // 判断不同键盘事件
            switch (evt.keyCode) {
                case 40:  //向下
                    com_topSch_cur++;
                    if (com_topSch_cur > key_length - 1) { com_topSch_cur = 0 } //如果超过最后一个，置0
                    com_setSchKey(com_topSch_cur);
                    return;
                case 38:  //向上
                    com_topSch_cur--;
                    if (com_topSch_cur < 0) { com_topSch_cur = key_length - 1 } //如果超过最前一个，置最后
                    com_setSchKey(com_topSch_cur);
                    return;
                case 27:  //Esc 收起
                    $("#lp-top-search .tip-ul").removeClass("show").addClass("hide").text("");
                    return;
            }
        }
    });
    // 传入i设置样式和输值到表单 给keydown事件调用
    function com_setSchKey(i) {
        // 设置样式
        $("#lp-top-search .tip-ul .search-li").removeClass("key");
        $("#lp-top-search .tip-ul .search-li").eq(i).addClass("key");
        // 输值
        $("#lp-top-search form input").val($(".tip-ul .search-li").eq(i).text());
    }

    // 点击其他位置收起自动补全菜单
    $(document).click(function () { $("#lp-top-search .tip-ul").removeClass("show").addClass("hide").text(""); })
    /**
    *头部搜索框focus样式
    */
    $("#lp-top-search input").focusin(function () {
        $(this).parent("form").children("button").addClass("hov");
        $(this).addClass("hov");
    })
    $("#lp-top-search input").focusout(function () {
        返回
        $(this).parent("form").children("button").removeClass("hov");
        $(this).removeClass("hov");
    })
    // 搜索框自动补全 END


    // 返回顶部
    $('#gotoTop').click(function () {
        $("html, body").animate({ scrollTop: 0 }, {
            duration: 600,
            easing: 'easeInExpo'
        });
        var self = this;
        this.className += ' ' + "launch";
        setTimeout(function () {
            self.className = 'show';
        }, 800)
    });
    $(window).scroll(function () {
        if ($(window).scrollTop() > 45) {
            $('#gotoTop').slideDown(450);
        } else {
            $('#gotoTop').slideUp(450);
        }
    })



    /**
    *右边栏模块
    */

    // 业界快讯选项卡
    $("#lp_right_select .ms-top ul li a").mouseover(function () {
        var num = $("#lp_right_select .ms-top ul li a").index($(this));
        var this1 = $(this);
        $("#lp_right_select .ms-main .bd").css({ "display": "none" });
        $("#lp_right_select .ms-main .bd").eq(num).css({ "display": "block" });
        $("#lp_right_select .ms-top .bar").stop().animate({ "left": 10 + num * 79 }, { easing: 'easeInExpo', duration: 200, complete: function () {
            $("#lp_right_select .ms-top ul li a").removeClass("cur")
            this1.addClass("cur");
        } 
        })
})
$("#lp_right_select2 .ms-top ul li a").mouseover(function () {
    var num = $("#lp_right_select2 .ms-top ul li a").index($(this));
    var this1 = $(this);
    $("#lp_right_select2 .ms-main .bd").css({ "display": "none" });
    $("#lp_right_select2 .ms-main .bd").eq(num).css({ "display": "block" });
    $("#lp_right_select2 .ms-top .bar").stop().animate({ "left": 10 + num * 79 }, { easing: 'easeInExpo', duration: 200, complete: function () {
        $("#lp_right_select2 .ms-top ul li a").removeClass("cur")
        this1.addClass("cur");
    }
    })
})
    var yjkxFadeIn = function () {
        var list = $("#lp_right_select .ms-main .bd-yjkx ul li"),
        length = list.length,
        speed = 550,
        time = 80,
        // timeadd = 10,
        maxCount = length - 1,
        minCount = 0,
        curCount = minCount,
        initLeft = 20,
        initLeftInterval = 5,
        initOpcity = 0,
        endOpcity = 1;

        var fade = function () {
            var sitv = setInterval(function () {
                if (curCount > maxCount) {
                    clearInterval(sitv);
                }
                list.eq(curCount).fadeIn(speed);
                curCount++;
            }, time)
        }


        var star = function () {
            // init();
            fade();
        }

        star();
    }
    yjkxFadeIn();

    // 关于我们旋转
    $("#lp_right_us .au-main .btn").hover(function () {
        $(this).addClass("hover");
    }, function () {
        $(this).removeClass("hover");
    })
    $("#lp_right_us .au-main .btn-wc").hover(function () {
        $(".btn-wc-ewm").addClass("show");
    }, function () {
        $(".btn-wc-ewm").removeClass("show");
    })

    // 标签色彩
    var tagColors = ["#CFCACA", '#CED3CF', '#BFCDCD', '#C4DAD5', '#C4EACD', '#FDD12D', '#D1CB6D', '#E3D670', '#CDCDB4', '#CD919E', '#C2C1BC', '#B3B2A3', '#83838D', '#2C464F', '#FFB2A8', '#C2AFA1', '#8C7E7E', '#CDB5CD', '#C9B96F', '#B6985B'];
    var tagColorsLen = tagColors.length;
    for (var i = 0; i <= $(".colorTags").length; i++) {
        var index = Math.ceil(Math.random() * tagColorsLen);
        $(".colorTags").eq(i - 1).css({ "background": tagColors[index - 1] })
    }
});

//更新点击数，差评，好评
function updateArticleNum(id, moduleType, cmd) {

    $.ajax({
        type: "get",
        dataType: "json",
        data: "time=" + (new Date().getTime()),
        url: "/" + id + "/" + moduleType + "/" + cmd,
        error: function (XmlHttpRequest, textStatus, errorThrown) { alert(errorThrown); alert(textStatus); alert(XmlHttpRequest.responseText); },
        success: function (d) {
            switch (d.result) {
                case '0':
                    alert("提交失败");
                    break;
                case '1':
                    if (cmd == "upsupport") {
                        var _this = $(".article-left .like-btn");
                        // var _thisJs = this;
                        var num = _this.children("span").text();
                        _this.children("span").text(Number(num) + 1);
                        _this.addClass("love-animate").attr("title", "已喜欢");
                        _loveYes = true;
                        setTimeout(function () {
                            _this.removeClass('love-animate').addClass('love-yes');
                        }, 500);
                        _this.bind("click", function () { void (0); });
                    }
                    else if (cmd == "upagainst") {
                        var num = 0;

                        num = $("#bury_num").html()
                        num = parseInt(num) + 1;
                        $("#bury_num").html(num);
                        $("#digg_tip").html("反对成功");
                        $("#buryit").bind("click", function () { void (0); });
                    }

                    break;

                case "-1":
                    alert(d.returnval);
                    break;
            }
        }
    });
}


